home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / TCL1 / DYNAMICA / CBIGARRA.C < prev    next >
Text File  |  1990-05-30  |  4KB  |  171 lines

  1. /******************************************************************************
  2.  
  3.  CBigArray.c
  4.  
  5. ******************************************************************************/
  6.  
  7. #include "CBigArray.h"
  8.  
  9. /*---------------------------------------------------------------------------*/
  10.  
  11. void    CBigArray::IBigArray(void)
  12. {
  13.     hData = NULL;
  14.     amf.nElements = 0;
  15.     amf.nElementSize = 0;
  16. }
  17.  
  18. /*---------------------------------------------------------------------------*/
  19.  
  20. Boolean    CBigArray::CreateData(long nElements,int nElementSizeIn,Handle hDataIn)
  21. {
  22.     amf.nElements = nElements;
  23.     amf.nElementSize = nElementSizeIn;
  24.     
  25.     if (hDataIn == NULL) {
  26.         gApplication->RequestMemory(FALSE, TRUE);
  27.         hData = NewHandleClear((long) amf.nElementSize * (long) nElements);
  28.         gApplication->RequestMemory(FALSE, TRUE);
  29.     }
  30.     else
  31.         hData = hDataIn;
  32.         
  33.     if (hData == NULL) return FALSE;
  34.     
  35.     return TRUE;
  36. }
  37.  
  38. /*---------------------------------------------------------------------------*/
  39.  
  40. Boolean    CBigArray::ResizeArray(long nNewNumberOfElements)
  41. {
  42.     OSErr    nError;
  43.     
  44.     SetHandleSize(hData,nNewNumberOfElements * amf.nElementSize);
  45.     nError = MemError();
  46.     if (nError == NoErr) {
  47.         amf.nElements = nNewNumberOfElements;
  48.         return TRUE;
  49.     } else
  50.         return FALSE;
  51. }
  52.  
  53. /*---------------------------------------------------------------------------*/
  54.  
  55. void    CBigArray::Dispose(void) /* OVERRIDE */
  56. {
  57.     if (hData != NULL) DisposHandle(hData);
  58.     inherited::Dispose();
  59. }
  60.  
  61. /*---------------------------------------------------------------------------*/
  62.  
  63. void    CBigArray::GetValue(long nIndex,char *pDestination)
  64. {
  65.     register char    *pData;
  66.     register int    i;
  67.     
  68.     HLock(hData);
  69.     pData= (char *) *hData;
  70.     pData += Offset(nIndex); 
  71.     
  72.     for (i=0; i < amf.nElementSize; ++i)
  73.       *pDestination++ = *pData++;
  74.     HUnlock(hData);
  75.     return;
  76. }
  77.  
  78. /*---------------------------------------------------------------------------*/
  79.  
  80. void    CBigArray::SetValue(long nIndex,char *pSource)
  81. {
  82.     register char    *pData;
  83.     register int    i;
  84.     
  85.     HLock(hData);
  86.     
  87.     pData= (char *) *hData;
  88.     pData += Offset(nIndex);
  89.     
  90.     for (i=0; i < amf.nElementSize; ++i) 
  91.        *pData++ = *pSource++;
  92.     HUnlock(hData);
  93.     return;
  94. }
  95.  
  96. /*---------------------------------------------------------------------------*/
  97.  
  98. OSErr    CBigArray::SaveData(CDataFile *datafile)
  99. {
  100.     AMFData        amfLocal;    /* Used to avoid implicit dereferencing errors    */
  101.     OSErr        nError;
  102.     long        lDataSize;
  103.     
  104.     /*-- Write array mapping data --*/
  105.     amfLocal = amf;
  106.     nError = datafile->WriteSome((char *) &amfLocal,(long) sizeof(AMFData));
  107.     if (nError != NoErr) return nError;
  108.     
  109.     /*-- Write array --*/
  110.     lDataSize = (long) GetHandleSize(hData);
  111.     HLock(hData);
  112.     nError = datafile->WriteSome((char *) *hData, lDataSize);
  113.     HUnlock(hData);
  114.     return nError;
  115. }
  116.  
  117. /*---------------------------------------------------------------------------*/
  118.  
  119. OSErr     CBigArray::LoadData(CDataFile *datafile)
  120. {
  121.     AMFData        amfLocal;    /* Used to avoid implicit dereferencing errors    */
  122.     OSErr        nError;
  123.     long        nHandleSize;
  124.     
  125.     /*-- Read array mapping data --*/
  126.     nError = datafile->ReadSome((char *) &amfLocal, (long) sizeof(AMFData));
  127.     if (nError != NoErr) return nError;
  128.     amf = amfLocal;
  129.     
  130.     /*-- Allocate Array --*/
  131.     nHandleSize = (long) amf.nElementSize * (long) amf.nElements;
  132.     
  133.     gApplication->RequestMemory(FALSE, TRUE);
  134.     hData = NewHandle(nHandleSize);
  135.     gApplication->RequestMemory(FALSE, FALSE);
  136.     if (hData == NULL) return MemError();
  137.     
  138.     /*-- Read array --*/
  139.     HLock(hData);
  140.     nError = datafile->ReadSome((char *) *hData,nHandleSize);
  141.     HUnlock(hData);
  142.     
  143.     return nError;
  144. }
  145.  
  146. /*---------------------------------------------------------------------------*/
  147.  
  148. void    CBigArray::GetDimensions(long *nElements, int *nElementSizeOut)
  149. {
  150.      *nElements = amf.nElements;
  151.      *nElementSizeOut = amf.nElementSize;
  152. }
  153.  
  154. /*---------------------------------------------------------------------------*/
  155.  
  156. Handle    CBigArray::GetData(void)
  157. {
  158.     return hData;
  159. }
  160.  
  161. /*---------------------------------------------------------------------------*/
  162.  
  163. long    CBigArray::Offset(long nIndex)
  164. {
  165.     return (long) nIndex * (long) amf.nElementSize; 
  166. }
  167.  
  168. /*---------------------------------------------------------------------------*/
  169.  
  170.  
  171.